#coding=utf-8
from django.core.cache import cache
from datiandou import settings
from datiandouweb.properties.Properties import Properties
class i18nMiddleware(object):
    context_data = {}


    def process_template_response(self, request, response):

        reqlanguage= str(request.META.get('HTTP_ACCEPT_LANGUAGE')).split(",")[0].lower()
        message_var=dict({})
        print("HTTP_ACCEPT_LANGUAGE:"+reqlanguage)
        labelinfo=cache.get('messageinfo')
        print("HTTP_ACCEPT_LANGUAGE:"+reqlanguage)
        labelinfo=cache.get('messageinfo')
        if not labelinfo:
            prop = Properties()
            messageinfo = dict()
            languages=prop.LANGUAGE_FILE_DICT.keys()
            for language in languages:
                print("语言x:"+str(language))
                language_message = prop.loadDiction(language)
                messageinfo[language]=language_message
            cache.set('messageinfo',messageinfo,24*60*60)
            labelinfo=messageinfo
            print(labelinfo)
        print(reqlanguage)
        onemessage=labelinfo[reqlanguage]
        print("onex"+str(onemessage))
        if  hasattr(response, 'context_data'):
            response.context_data['i18xnmessage']=onemessage
        message_var.setdefault('i18nmessage',onemessage)
        return response